﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using Bzzb.Util;
using FreeSql;
using FreeSql.Internal.Model;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class KeShiHuaYunWeiController : BaseController
    {
        public static int INDEX1 = -1;
        public static int INDEX2 = -1;
        public static int INDEX3 = -1;
        public static int INDEX4 = -1;
        private readonly ILogger<KeShiHuaYunWeiController> _logger;
        public KeShiHuaYunWeiController(ILogger<KeShiHuaYunWeiController> logger, IWebHostEnvironment env, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
            KeShiHuaYunWeiController.INDEX1++;
            if (KeShiHuaYunWeiController.INDEX1 > JiShuQi.XIANLU1.Count -1)
            {
                KeShiHuaYunWeiController.INDEX1 = -1;
            }
            KeShiHuaYunWeiController.INDEX2++;
            if (KeShiHuaYunWeiController.INDEX2 > JiShuQi.XIANLU2.Count -1)
            {
                KeShiHuaYunWeiController.INDEX2 = -1;
            }
            KeShiHuaYunWeiController.INDEX3++;
            if (KeShiHuaYunWeiController.INDEX3 > JiShuQi.XIANLU3.Count -1)
            {
                KeShiHuaYunWeiController.INDEX3 = -1;
            }
            KeShiHuaYunWeiController.INDEX4++;
            if (KeShiHuaYunWeiController.INDEX4 > JiShuQi.XIANLU4.Count -1)
            {
                KeShiHuaYunWeiController.INDEX4 = -1;
            }
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index()
        {
            JArray data = new();

            JObject jyc1 = new();
            JiaYouChe j1 = await _fsql.Select<JiaYouChe>().Where(jyc => jyc.Id == 1).ToOneAsync();
            YiChang yj1 = await _fsql.Select<YiChang>().Where(yc => yc.Bzzbtid == 1 && yc.Bzzbid == 2 && yc.Jtid == 1 && yc.Gjid == 0).OrderBy("id desc").ToOneAsync();
            jyc1.Add("id", j1.Id);
            jyc1.Add("clmc", j1.Clmc);
            jyc1.Add("cph", j1.Cph);
            jyc1.Add("kzxmc", yj1?.Kzxmc);
            jyc1.Add("message", yj1?.Message);
            jyc1.Add("level", yj1?.Level);
            jyc1.Add("position", JiShuQi.XIANLU1[KeShiHuaYunWeiController.INDEX1]);

            JObject jyc2 = new();
            JiaYouChe j2 = await _fsql.Select<JiaYouChe>().Where(jyc => jyc.Id == 2).ToOneAsync();
            YiChang yj2 = await _fsql.Select<YiChang>().Where(yc => yc.Bzzbtid == 1 && yc.Bzzbid == 2 && yc.Jtid == 2 && yc.Gjid == 0).OrderBy("id desc").ToOneAsync();
            jyc2.Add("id", j2.Id);
            jyc2.Add("clmc", j2.Clmc);
            jyc2.Add("cph", j2.Cph);
            jyc2.Add("kzxmc", yj2?.Kzxmc);
            jyc2.Add("message", yj2?.Message);
            jyc2.Add("level", yj2?.Level);
            jyc2.Add("position", JiShuQi.XIANLU2[KeShiHuaYunWeiController.INDEX2]);

            JObject dyc1 = new();
            DianYuanChe d1 = await _fsql.Select<DianYuanChe>().Where(dyc => dyc.Id == 1).ToOneAsync();
            YiChang yd1 = await _fsql.Select<YiChang>().Where(yc => yc.Bzzbtid == 1 && yc.Bzzbid == 1 && yc.Jtid == 1 && yc.Gjid == 0).OrderBy("id desc").ToOneAsync();
            dyc1.Add("id", d1.Id);
            dyc1.Add("clmc", d1.Clmc);
            dyc1.Add("cph", d1.Cph);
            dyc1.Add("kzxmc", yd1?.Kzxmc);
            dyc1.Add("message", yd1?.Message);
            dyc1.Add("level", yd1?.Level);
            dyc1.Add("position", JiShuQi.XIANLU3[KeShiHuaYunWeiController.INDEX3]);

            JObject dyc3 = new();
            DianYuanChe d3 = await _fsql.Select<DianYuanChe>().Where(dyc => dyc.Id == 3).ToOneAsync();
            YiChang yd3 = await _fsql.Select<YiChang>().Where(yc => yc.Bzzbtid == 1 && yc.Bzzbid == 1 && yc.Jtid == 3 && yc.Gjid == 0).OrderBy("id desc").ToOneAsync();
            dyc3.Add("id", d3.Id);
            dyc3.Add("clmc", d3.Clmc);
            dyc3.Add("cph", d3.Cph);
            dyc3.Add("kzxmc", yd3?.Kzxmc);
            dyc3.Add("message", yd3?.Message);
            dyc3.Add("level", yd3?.Level);
            dyc3.Add("position", JiShuQi.XIANLU4[KeShiHuaYunWeiController.INDEX4]);

            data.Add(jyc1);
            data.Add(jyc2);
            data.Add(dyc1);
            data.Add(dyc3);

            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Lb() {
            JArray data = new();

            JObject jyc1 = new();
            JiaYouChe j1 = await _fsql.Select<JiaYouChe>().Where(jyc => jyc.Id == 1).ToOneAsync();
            YiChang yj1 = await _fsql.Select<YiChang>().Where(yc => yc.Bzzbtid == 1 && yc.Bzzbid == 2 && yc.Jtid == 1 && yc.Gjid == 0).OrderBy("id desc").ToOneAsync();
            jyc1.Add("id", j1.Id);
            jyc1.Add("clmc", j1.Clmc);
            jyc1.Add("cph", j1.Cph);
            jyc1.Add("kzxmc", yj1?.Kzxmc);
            jyc1.Add("message", yj1?.Message);
            jyc1.Add("level", yj1?.Level);
            jyc1.Add("position", JiShuQi.XIANLU1[KeShiHuaYunWeiController.INDEX1]);

            JObject jyc2 = new();
            JiaYouChe j2 = await _fsql.Select<JiaYouChe>().Where(jyc => jyc.Id == 2).ToOneAsync();
            YiChang yj2 = await _fsql.Select<YiChang>().Where(yc => yc.Bzzbtid == 1 && yc.Bzzbid == 2 && yc.Jtid == 2 && yc.Gjid == 0).OrderBy("id desc").ToOneAsync();
            jyc2.Add("id", j2.Id);
            jyc2.Add("clmc", j2.Clmc);
            jyc2.Add("cph", j2.Cph);
            jyc2.Add("kzxmc", yj2?.Kzxmc);
            jyc2.Add("message", yj2?.Message);
            jyc2.Add("level", yj2?.Level);
            jyc2.Add("position", JiShuQi.XIANLU2[KeShiHuaYunWeiController.INDEX2]);

            JObject dyc1 = new();
            DianYuanChe d1 = await _fsql.Select<DianYuanChe>().Where(dyc => dyc.Id == 1).ToOneAsync();
            YiChang yd1 = await _fsql.Select<YiChang>().Where(yc => yc.Bzzbtid == 1 && yc.Bzzbid == 1 && yc.Jtid == 1 && yc.Gjid == 0).OrderBy("id desc").ToOneAsync();
            dyc1.Add("id", d1.Id);
            dyc1.Add("clmc", d1.Clmc);
            dyc1.Add("cph", d1.Cph);
            dyc1.Add("kzxmc", yd1?.Kzxmc);
            dyc1.Add("message", yd1?.Message);
            dyc1.Add("level", yd1?.Level);
            dyc1.Add("position", JiShuQi.XIANLU3[KeShiHuaYunWeiController.INDEX3]);

            JObject dyc3 = new();
            DianYuanChe d3 = await _fsql.Select<DianYuanChe>().Where(dyc => dyc.Id == 3).ToOneAsync();
            YiChang yd3 = await _fsql.Select<YiChang>().Where(yc => yc.Bzzbtid == 1 && yc.Bzzbid == 1 && yc.Jtid == 3 && yc.Gjid == 0).OrderBy("id desc").ToOneAsync();
            dyc3.Add("id", d3.Id);
            dyc3.Add("clmc", d3.Clmc);
            dyc3.Add("cph", d3.Cph);
            dyc3.Add("kzxmc", yd3?.Kzxmc);
            dyc3.Add("message", yd3?.Message);
            dyc3.Add("level", yd3?.Level);
            dyc3.Add("position", JiShuQi.XIANLU4[KeShiHuaYunWeiController.INDEX4]);

            data.Add(jyc1);
            data.Add(jyc2);
            data.Add(dyc1);
            data.Add(dyc3);

            return RenderJson(data: data);
        }
    }
}
